﻿using demo1.Models.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace demo1.Service
{
    public class UserService : IUserService
    {
        private readonly WebEnterpriseIIContext _db;
        public UserService(WebEnterpriseIIContext db)
        {
            _db = db;
        }
        public User GetUserByUserName(string userName)
        {
            return _db.Users.FirstOrDefault(x => x.UserName == userName);
        }
        public bool ExistUserByUserName(string userName)
        {
            return _db.Users.Any(x => x.UserName == userName);
        }
        public int AddNewUser(string userName, string email, string nickname, string password)
        {
            var user = new User()
            {
                UserName = userName,
                Email = email,
                NickName = nickname,
                Password = password,
                CreateTime = DateTime.Now,
                Salt = 0,
                UserLevel = 1,
            };
            _db.Users.Add(user);
            var row = _db.SaveChanges();
            if (row > 0)
                return user.UserId;
            return 0;
        }
    }
}
